home *** CD-ROM | disk | FTP | other *** search
/ PCMania 81 / PCMania CD81_1.iso / PCMANIA / demosc81 / raytrac.c < prev    next >
C/C++ Source or Header  |  1999-04-22  |  5KB  |  185 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <dos.h>
  5. #include <conio.h>
  6. #include <i86.h>
  7. #include <string.h>
  8. #include <math.h>
  9. #include "vesalint.h"
  10. #include "affine.c"
  11.  
  12. #pragma aux Set_Mode=\
  13. "int    10h"\
  14. modify  [ax]\
  15. parm    [ax]
  16.  
  17. int offset=0;
  18.  
  19. typedef struct RT_camera
  20. {
  21.     float xpos,ypos,zpos,d;
  22. }RT_CAMERA;
  23.  
  24. typedef struct RT_esfera 
  25. {
  26.     float  xpos,ypos,zpos,radi;
  27.     float  coeff;
  28.     VECTOR ambient,diffuse,specular;
  29. }RT_ESFERA;
  30.  
  31. typedef struct RT_luz
  32. {
  33.     float xpos,ypos,zpos;
  34.     VECTOR color;
  35. }RT_LUZ;
  36.  
  37. typedef struct RT_escena
  38. {
  39.     int num_luz,num_esfera,num_camera;
  40.     RT_ESFERA *esferas;
  41.     RT_LUZ    *luces;
  42.     RT_CAMERA *cameras;
  43.  
  44.     VECTOR    ambient;
  45. }RT_ESCENA;     
  46.  
  47. void RT_render(RT_ESCENA *esc, unsigned char *dest)
  48. {
  49.     int    xc,yc,sc,ppos,min_esfera;
  50.     float  bpart,disc,t1,t2,min_t;
  51.     VECTOR ray,color;
  52.  
  53.     ppos=0;
  54.     for (sc=0;sc<esc->num_esfera;sc++)
  55.         esc->esferas[sc].coeff=pow(esc->cameras->xpos-esc->esferas[sc].xpos,2)+
  56.                                pow(esc->cameras->ypos-esc->esferas[sc].ypos,2)+
  57.                                pow(esc->cameras->zpos-esc->esferas[sc].zpos,2)-
  58.                                pow(esc->esferas[sc].radi,2);
  59.  
  60.     for (yc=-100;yc<100;yc++)
  61.     {
  62.     for (xc=-160;xc<160;xc++)
  63.     {
  64.         min_t=1000000.0;
  65.         min_esfera=-1;
  66.         ray.v1=xc;
  67.         ray.v2=yc;
  68.         ray.v3=esc->cameras->d;
  69.         ray=unit_vtr(ray);
  70.         for (sc=0;sc<esc->num_esfera;sc++)
  71.         {
  72.             bpart=(esc->cameras->xpos-esc->esferas[sc].xpos)*ray.v1+
  73.                   (esc->cameras->ypos-esc->esferas[sc].ypos)*ray.v2+
  74.                   (esc->cameras->zpos-esc->esferas[sc].zpos)*ray.v3;
  75.             disc=pow(bpart,2)-esc->esferas[sc].coeff;
  76.             if (disc>0)
  77.             {
  78.                 disc=sqrt(disc);
  79.                 t1=-bpart+disc;
  80.                 t2=-bpart-disc;
  81.                 if ((t1>0)&&(t1<min_t))
  82.                 {
  83.                     min_t=t1;
  84.                     min_esfera=sc;
  85.                 }
  86.                 if ((t2>0)&&(t2<min_t))
  87.                 {
  88.                     min_t=t2;
  89.                     min_esfera=sc;
  90.                 }
  91.             }
  92.         }
  93.         if (min_esfera>=0)
  94.         {
  95.             color.v1=esc->esferas[min_esfera].ambient.v1*esc->ambient.v1;
  96.             color.v2=esc->esferas[min_esfera].ambient.v2*esc->ambient.v2;
  97.             color.v3=esc->esferas[min_esfera].ambient.v3*esc->ambient.v3;
  98.             dest[ppos  ]=min(color.v3,254);
  99.             dest[ppos+1]=min(color.v2,254);
  100.             dest[ppos+2]=min(color.v1,254);
  101.         }
  102.         else
  103.         {
  104.             dest[ppos  ]=0;
  105.             dest[ppos+1]=0;
  106.             dest[ppos+2]=0;
  107.         }
  108.         ppos+=3;
  109.     }
  110.     }
  111. }
  112.  
  113. RT_ESCENA *ray_testing;
  114. unsigned char *Virtpant1;
  115. void main(int argc, char *argv[])
  116. {
  117.     float movi=0;
  118.     ray_testing=malloc(sizeof(RT_ESCENA));
  119.     ray_testing->num_esfera =2;
  120.     ray_testing->num_camera =1;
  121.     ray_testing->num_luz    =0;
  122.     ray_testing->esferas=malloc(sizeof (RT_ESFERA)*ray_testing->num_esfera);
  123.     ray_testing->cameras=malloc(sizeof (RT_CAMERA)*ray_testing->num_camera);
  124.     ray_testing->luces  =malloc(sizeof (RT_LUZ   )*ray_testing->num_luz);
  125.  
  126.     ray_testing->ambient.v1=128;
  127.     ray_testing->ambient.v2=128;
  128.     ray_testing->ambient.v3=128;
  129.  
  130.     ray_testing->esferas[0].xpos=100;
  131.     ray_testing->esferas[0].ypos=0;
  132.     ray_testing->esferas[0].zpos=950;
  133.     ray_testing->esferas[0].radi=100;
  134.     ray_testing->esferas[0].ambient.v1=1;
  135.     ray_testing->esferas[0].ambient.v2=0;
  136.     ray_testing->esferas[0].ambient.v3=0;
  137.  
  138.     ray_testing->esferas[1].xpos=-100;
  139.     ray_testing->esferas[1].ypos=0;
  140.     ray_testing->esferas[1].zpos=1000;
  141.     ray_testing->esferas[1].radi=150;
  142.     ray_testing->esferas[1].ambient.v1=0;
  143.     ray_testing->esferas[1].ambient.v2=0;
  144.     ray_testing->esferas[1].ambient.v3=1;
  145.  
  146.     ray_testing->cameras[0].xpos=0;   
  147.     ray_testing->cameras[0].ypos=0;   
  148.     ray_testing->cameras[0].zpos=0;
  149.     ray_testing->cameras[0].d=256;
  150.  
  151.     Virtpant1=malloc(320*200*3);
  152.     Active.Screen.Width=320;
  153.     Active.Screen.Height=200;
  154.     Active.Screen.bpp=24;
  155.  
  156.     
  157.     if(argc>=2)
  158.     {
  159.         Active.Screen.bpp=atoi(argv[1]);
  160.         if (atoi(argv[1])==-1)
  161.         Active.Screen.Height=400;
  162.     }
  163.  
  164.  
  165.     Active.Work.Width=320;
  166.     Active.Work.Height=200;
  167.     Active.Work.bpp=24;
  168.  
  169.     Active.EnableLFB=1;
  170.     if (!InitSetMode())return;
  171.     
  172.     while (!kbhit())
  173.     {
  174.         ray_testing->esferas[0].xpos=200*cos(movi/20);
  175.         movi++;
  176.         RT_render(ray_testing,Virtpant1);
  177.         Flip(Virtpant1);
  178.     }
  179.     getch();
  180.     Free_Memory();
  181.     Set_Mode(0x3);
  182. }
  183.  
  184.  
  185.